<template>
  <div>
    <div v-show="!isShowDetail">
      <jxf-titletip :title="title" @refreshPage="refreshFuc"></jxf-titletip>
      <el-card class="mt20">
        <el-form :inline="true" :model="searchInfo" ref="searchInfo">
          <el-form-item label="采购商企业名:" prop="enterpriseName">
            <el-input v-model.trim="searchInfo.enterpriseName" clearable @keyup.enter.native="enterNextInput('telphone')" ref="input-enterpriseName"></el-input>
          </el-form-item>
          <el-form-item label="采购商账号:" prop="telphone">
            <el-input v-model.trim="searchInfo.telphone" clearable @keyup.enter.native="onSearch" ref="input-telphone"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSearch()">搜索</el-button>
          </el-form-item>
          <el-form-item>
            <el-button type="info" @click="resetForm('searchInfo')">重置</el-button>
          </el-form-item>
        </el-form>
      </el-card>
      <el-card class="jxf-table-card mt20">
        <div slot="header" class="clearfix">
          <span>数据列表</span>
          <div>
            <el-button type="primary" @click="exportExcel" :loading="isSuccess">导出</el-button>
          </div>
        </div>
        <jxf-table class="" :table="dataTable">
          <template slot="totalAmount" slot-scope="merchant">
            <div>
              <div>￥{{merchant.row.totalAmount && merchant.row.totalAmount != 0 ? formatMoney.twoDecimal(merchant.row.totalAmount) : '0.00'}}</div>
            </div>
          </template>
          <template slot="operation" slot-scope="merchant">
            <div>
              <el-button type="text" @click="watchDetail(merchant.row)">查看详情</el-button>
            </div>
          </template>
        </jxf-table>
      </el-card>
      <jxf-pagination class="mt20" :pageTotal="pageTotal" :currentPage.sync='currentPage' :pageSize.sync="pageSize"
                      @currentPageChange="currentPageChange" @pageSizeChange="pageSizeChange"></jxf-pagination>
    </div>
    <PayableUnpaid v-if="isShowDetail" :dataInfo="dataInfo" @goBack="goBack"></PayableUnpaid>
  </div>
</template>

<script>
  import PayableUnpaid from './components/PayableUnpaid'
  import formatMoney from '@/assets/js/formatMoney'
  export default {
    name: "MerchantStatistics",
    components: { PayableUnpaid },
    data() {
      return {
        title: "采购商应付未付统计",
        searchInfo: {
          enterpriseName: "",
          telphone: ""
        },
        formatMoney: formatMoney,
        isShowDetail: false,
        isSuccess: false,
        dataInfo: {},
        dataTable: {
          border:true,
          loading:false,
          hasSelect:false,
          hasOperation:false,
          tr: [
            {
              id: '1',
              minWidth: '140',
              label: '采购商企业名称',
              prop: 'enterpriseName'
            },
            {
              id: '2',
              minWidth: '100',
              label: '采购商账号',
              prop: 'telphone'
            },
            {
              id: '3',
              minWidth: '100',
              label: '未付订单数',
              prop: 'count'
            },
            {
              id: '4',
              minWidth: '120',
              label: '未付订单总额',
              prop: 'totalAmount',
              show: 'template'
            },
            {
              id: '5',
              label: '操作',
              show: 'template',
              minWidth:"200",
              prop: 'operation'
            }
          ],
          operation: [],
          data: []
        },
        pageTotal:0,
        pageSize:10,
        currentPage:1
      }
    },
    methods: {
      // 导出
      exportExcel() {
        this.isSuccess = true;
        let data = {
          ...this.searchInfo
        }
        ajax.export(apis.finance.exporMerchantStatisList, data).then((res) => {
          this.isSuccess = false;
          if(res.code) {
            if(res.code === "10000000") {

            } else {
              this.$message.error(res.message)
            }
          }
        })
      },
      enterNextInput(inputName) {
        this.$refs["input-" + inputName].focus();
      },
      refreshFuc() {
        this.resetForm('searchInfo')
        this.currentPage = 1;
        this.pageSize = 10
        this.getData()
      },
      goBack() {
        this.isShowDetail = false;
      },
      getData() {
        this.dataTable.loading = true;
        let data = {
          ...this.searchInfo,
          page: this.currentPage,
          pageSize: this.pageSize
        };
        ajax.post(apis.finance.getMerchantStatisticList, data).then(res => {
          if(res.code === "10000000") {
            this.dataTable.data = res.data.dataList;
            this.pageTotal = res.data.total
          } else {
            this.$message.error(res.message)
          }
          this.dataTable.loading = false;
        })
      },
      onSearch() {
        this.currentPage = 1;
        this.getData()
      },
      resetForm(formName) {
        this.$refs[formName].resetFields()
      },
      // 切换页面
      currentPageChange(pageNo) {
        this.currentPage = pageNo
      },
      // 切换页面条数
      pageSizeChange(pageNo) {
        this.pageSize = pageNo
      },
      watchDetail(obj) {
        let data = {
          merchantId: obj.merchantId,
          page: 1,
          pageSize: 10
        }
        ajax.post(apis.finance.getMerStatisticsDetail, data).then(res => {
          if(res.code == "10000000") {
            this.dataInfo = res.data;
            this.dataInfo.merchantId = obj.merchantId
            this.dataInfo.enterpriseName = obj.enterpriseName
            this.isShowDetail = true;
          } else {
            this.$message.error(res.message)
          }
        })
      }
    },
    created() {
      this.onSearch()
    }
  }
</script>

<style lang="scss" scoped>

</style>
